var Loader = (function () {
    function Loader() {
    }
    Loader.LoadScript = function (addr, callback) {
        if (Loader.loadedScripts[addr] != null) {
            callback();
            return false;
        }
        var scrEl = document.createElement("script");
        scrEl.type = "text/javascript";
        scrEl.onload = function (e) {
            Loader.loadedScripts[addr] = Date.now;
            callback();
        };
        scrEl.src = addr;
        document.head.appendChild(scrEl);
    };
    Loader.loadedScripts = {
        "console-manage.js": Date.now
    };
    return Loader;
})();

var Pjs2Loader = (function () {
    function Pjs2Loader() {
    }
    Pjs2Loader.LoadDefinitons = function (addr, rnt) {
        $.ajax({
            url: addr,
            success: function (d) {
                if (typeof d != "string") {
                    d = JSON.stringify(d);
                }
                Pjs2Loader.LoadDefs(d, rnt);
            },
            error: function (e) {
                if (e.status == 200)
                    Pjs2Loader.LoadDefs(e.responseText, rnt);
            }
        });
    };

    Pjs2Loader.LoadDefs = function (json, rnt) {
        var _this = this;
        var d = $.parseJSON(json);
        console.log(json);
        d["functions"].forEach(function (f) {
            if (f["definedIn"] && f["name"]) {
                var name = f["name"];
                var funcName = f["funcName"];
                if (!funcName) {
                    funcName = name.replace(/\-(.)/, function (a, b) {
                        return b.toUpperCase();
                    });
                    funcName = funcName.charAt(0).toLowerCase() + funcName.substr(1);
                }
                var defin = f["definedIn"];
                var defin = d["fileAliases"][defin] || defin;
                var funcDef = new Pjs2.FunctionDefinition();
                funcDef.name = name;
                funcDef.doFunc = _this.GetLazyDoFunction(defin, funcName, funcDef);
                funcDef.parameters = _this.GetParameters(f["parameters"]);
                rnt.AddFunction(funcDef);
            }
        });
        if (d["aliases"] && d["aliases"] instanceof Array)
            d["aliases"].forEach(function (a) {
                if (a.name && a.for)
                    rnt.AddAlias(a.name, a.for);
            });
    };

    Pjs2Loader.GetParameters = function (a) {
        var res = new Array();
        a.forEach(function (p) {
            var r = new Pjs2.FuncParameterDefinition(p["name"], !!p["required"], !!p["allowPipeline"]);
            res.push(r);
        });
        return res;
    };

    Pjs2Loader.GetLazyDoFunction = function (definedIn, funcName, definition) {
        var _this = this;
        return function (r, p) {
            if (!Loader.loadedScripts[definedIn]) {
                var callback = new Pjs2.ReturnCallback();
                callback.PostMessage("Loading script " + definedIn);
                Loader.LoadScript(definedIn, function () {
                    var f = _this.GetFuncByName(funcName);
                    definition.doFunc = function (r1, p1) {
                        return f.call(null, r1, p1);
                    };
                    callback.Return(definition.Call(r, p));
                });
                return callback;
            } else {
                var f = _this.GetFuncByName(funcName);
                definition.doFunc = function (r1, p1) {
                    return f.call(null, r1, p1);
                };
                return definition.Call(r, p);
            }
        };
    };

    Pjs2Loader.GetFuncByName = function (name) {
        return window[name];
    };
    return Pjs2Loader;
})();
//# sourceMappingURL=load.js.map
